package com.ebt.m.customer.drawable;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class FlagDrawable extends Drawable {

    public static final int[] COLORS = new int[]{ 0xff43d5e4, 0xfffccc67, 0xfff88db9};
    private int color = COLORS[0];

	private static final int COLORDRAWABLE_DIMENSION = 2;
	private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;

    private Path path = new Path();
    private Paint paint = new Paint();

//	private RectF mRectBounds = new RectF();
    private int bendWidth;
    private int bendHeight;
    private int width;
    private int height;

	public FlagDrawable(int bendWidth, int type) {
        this.bendWidth = bendWidth;
        this.color = COLORS[type % COLORS.length];
		init();
	}

	private void init(){
		setup();
	}
	
	@Override
	public void setBounds(int left, int top, int right, int bottom) {
		super.setBounds(left, top, right, bottom);
        Log.d("flagd","setBounds left:" + left + " top: " + top + " right: " + right + " bottom: " + bottom);
        width = right - left;
        height = bottom - top;
		setup();
	}

    public void setColor(int color){
        this.color = color;
    }
	
	@Override
	public void draw(Canvas canvas) {

        bendHeight = bendWidth * 5 / 3;
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        path.reset();
        path.moveTo(0, 0);
        path.quadTo(0, bendHeight / 4 * 1, bendWidth/2, bendHeight/2);
        path.quadTo(bendWidth, bendHeight / 4 * 3, bendWidth, bendHeight);
        path.lineTo(bendWidth, height - bendHeight);
        path.quadTo(bendWidth, height - bendHeight * 3 / 4, bendWidth/2, height - bendHeight / 2);
        path.quadTo(0, height - bendHeight * 1 / 4, 0, height);
        path.close();

        path.close();
        canvas.drawColor(0xffffffff);
        canvas.drawPath(path, paint);
	}

	@Override
	public int getIntrinsicWidth() {
		return width;
	}

	@Override
	public int getIntrinsicHeight() {
		return height;
	}

	@Override
	public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
	}

	@Override
	public int getOpacity() {
		return PixelFormat.TRANSLUCENT;
	}

	private void setup() {
        invalidateSelf();
    }
}